package org.stvd.common.utils;

import java.util.HashMap;
import java.util.Map;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import net.sf.json.JSONObject;

/**
 * 短信工具类
 * @author houzx
 */
public class SMSUtil {
    
    static {
        DySMSUtil dySMSUtil = new DySMSUtil();
        // 初始化为宁夏中小账号
        dySMSUtil.setAccessKeyId("LTAIqRE0vHyiQFVK");
        dySMSUtil.setAccessKeySecret("OJ4KEN1sGcnK6EQJ3eSaXn4xnPekyX");
    }

    @SuppressWarnings("static-access")
    public static SendSmsResponse alysendMessage(String sms_from ,String accessKeyId, String accessKeySecret,
            String phone_no, String sms_template, String param){
        SendSmsResponse response = null;
        try {
            DySMSUtil dySMSUtil = new DySMSUtil();
            if(accessKeyId!=null && !"".equals(accessKeyId)) {
                dySMSUtil.setAccessKeyId(accessKeyId);
            }
            if(accessKeySecret!=null && !"".equals(accessKeySecret)) {
                dySMSUtil.setAccessKeySecret(accessKeySecret);
            }
            response = dySMSUtil.sendSms(sms_from,phone_no,sms_template,param);
            System.out.println("短信接口返回的数据----------------");
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }
    
    /**
     * 通过阿里云短信平台发送短信
     * @param signName 短信平台签名，参见：{@linkplain org.stvd.common.enums.AliDayuEnum AliDayuEnum}
     * @param phoneNumber 接收短信的手机号
     * @param templateCode 短信模板code，参见：{@linkplain org.stvd.common.enums.AliDayuEnum AliDayuEnum}
     * @param templateParamMap 短信模板参数，所需参数参见：{@linkplain org.stvd.common.enums.AliDayuEnum AliDayuEnum}
     * @return keys[sendSmsResponse(可能没有这个key), errorMsg|success]
     */
    public static Map<String, String> sendMessageByAliYun(String signName, Long phoneNumber,
            String templateCode, Map<String, String> templateParamMap) {
        Map<String, String> map = new HashMap<>(2);

        if (signName == null || "".equals(signName)) {
            map.put("errorMsg", "短信签名丢失！");
            return map;
        }
        if (templateCode == null || "".equals(templateCode)) {
            map.put("errorMsg", "短信模板编码丢失！");
            return map;
        }
        if (phoneNumber == null) {
            map.put("errorMsg", "接收短信的手机号丢失！");
            return map;
        }
        if (templateParamMap==null || templateParamMap.size()<1) {
            map.put("errorMsg", "短信模板参数丢失！");
            return map;
        }

        SendSmsResponse response = null;
        try {
            response = DySMSUtil.sendSms(signName, phoneNumber.toString(),
                    templateCode, JSONObject.fromObject(templateParamMap).toString());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("errorMsg", "短信平台调用失败！");
            return map;
        }
        map.put("sendSmsResponse", JSONObject.fromObject(response).toString());
        if (!("OK".equals(response.getCode()))) {
            map.put("errorMsg", response.getMessage());
            return map;
        }

        map.put("success", "true");
        return map;
    }
}
